/**
 * ====================================================================
 *
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 * 
 * @author Christian
 * @since
 * @version 
 * @Date 26.04.2014
 */
package de.sisoft.gcjack.view.components;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import de.sisoft.gcjack.R;

/**
 *
 */
public class RecommendationButton extends Button {

    private String DEBUGTAG = "CustonButtonExample";

    // Keeps track of the current state, 0, 1, or 2
    private int _state;

    // Get the attributes created in recommendationattributes.xml
    private static final int[] STATE_ONE_SET = { R.attr.neutral };

    private static final int[] STATE_TWO_SET = { R.attr.recommended };

    private static final int[] STATE_THREE_SET = { R.attr.declined };

    // Constructors
    public RecommendationButton(Context context) {
	super(context);

	// Set the default state and text
	_state = 0;
	this.setBackgroundResource(R.drawable.button_neutral);
    }

    public RecommendationButton(Context context, AttributeSet attrs) {
	super(context, attrs);

	// Set the default state and text
	_state = 0;
	this.setBackgroundResource(R.drawable.button_neutral);
    }

    public RecommendationButton(Context context, AttributeSet attrs,
	    int defStyle) {
	super(context, attrs, defStyle);

	// Set the default state and text
	_state = 0;
	this.setBackgroundResource(R.drawable.button_neutral);
    }

    @Override
    public boolean performClick() {
	// Move to the next state
	nextState();

	return super.performClick();
    }

    // Generate the drawable needed for the current state
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
	// Add the number of states you have
	final int[] drawableState = super.onCreateDrawableState(extraSpace + 3);

	if (_state == 0) {
	    mergeDrawableStates(drawableState, STATE_ONE_SET);
	} else if (_state == 1) {
	    mergeDrawableStates(drawableState, STATE_TWO_SET);
	} else if (_state == 2) {
	    mergeDrawableStates(drawableState, STATE_THREE_SET);
	}

	return drawableState;
    }

    // Set current state, 0-2
    public void setState(int state) {
	if ((state > -1) && (state < 3)) {
	    // Log.d(DEBUGTAG, "   Setting Toggle state to " + state);
	    _state = state;
	    setButtonImage();
	}
    }

    // Returns current state
    public int getState() {
	return _state;
    }

    // Increases state, or loops to 0
    public void nextState() {
	_state++;

	// Loop around if at the last state
	if (_state > 2) {
	    _state = 0;
	}

	setButtonImage();
	// Log.d(DEBUGTAG, "   Setting Toggle state to " + _state);
	// showShortToast("ToggleState: " + _state);
    }

    // Decreases state, or loops to 2
    public void previousState() {
	_state--;

	// Loop around if at the first state
	if (_state < 0) {
	    _state = 2;
	}

	setButtonImage();
	Log.d(DEBUGTAG, "   Setting Toggle state to " + _state);
	// showShortToast("ToggleState: " + _state);
    }

    // Set the text displayed on the button
    private void setButtonImage() {
	switch (_state) {
	    case 0:
		this.setBackgroundResource(R.drawable.button_neutral);
		break;
	    case 1:
		this.setBackgroundResource(R.drawable.button_recommended);
		break;
	    case 2:
		this.setBackgroundResource(R.drawable.button_declined);
		break;
	    default:
		this.setText("N/A"); // Should never happen, but just in case
		break;
	}
    }

    // A method just to make using Toasts easier
    private void showShortToast(String s) {
	Toast.makeText(this.getContext(), s, Toast.LENGTH_SHORT).show();
    }
}
